package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.scraprecord.entity.Scraprecord;
import com.mbyte.easy.scraprecord.service.IScraprecordService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.util.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/scraprecord/scraprecord")
public class ScraprecordController extends BaseController  {

    private String prefix = "scraprecord/scraprecord/";

    @Autowired
    private IScraprecordService scraprecordService;
    @Autowired
    private IUserService userService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param scraprecord
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String scrapApplytimeSpace, String scrapCreatetimeSpace, Scraprecord scraprecord) {
        Page<Scraprecord> page = new Page<Scraprecord>(pageNo, pageSize);
        QueryWrapper<Scraprecord> queryWrapper = new QueryWrapper<Scraprecord>();
        if(!ObjectUtils.isEmpty(scraprecord.getScrapapplyId())) {
            queryWrapper = queryWrapper.like("scrapapply_id",scraprecord.getScrapapplyId());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getRecordId())) {
            queryWrapper = queryWrapper.like("record_id",scraprecord.getRecordId());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",scraprecord.getEquId());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",scraprecord.getEquName());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",scraprecord.getEquStatus());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",scraprecord.getEquType());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getScrapProson())) {
            queryWrapper = queryWrapper.like("scrap_proson",scraprecord.getScrapProson());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getScrapApplytime())) {
            queryWrapper = queryWrapper.like("scrap_applytime",scraprecord.getScrapApplytime());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getApprover())) {
            queryWrapper = queryWrapper.like("approver",scraprecord.getApprover());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getScrapReason())) {
            queryWrapper = queryWrapper.like("scrap_reason",scraprecord.getScrapReason());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getScrapCreatetime())) {
            queryWrapper = queryWrapper.like("scrap_createtime",scraprecord.getScrapCreatetime());
         }
        if(!ObjectUtils.isEmpty(scraprecord.getScrapRemake())) {
            queryWrapper = queryWrapper.like("scrap_remake",scraprecord.getScrapRemake());
         }
        List<Scraprecord> list  = scraprecordService.list();
        Map<Integer,String >  types= new HashMap<>();
        Map<Integer,String >  namess= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser users = userService.selectByUsername(list.get(i).getApprover().toString());
            types.put(list.get(i).getRecordId(), users.getAccount());

            SysUser user = userService.selectByUsername(list.get(i).getScrapProson().toString());
            namess.put(list.get(i).getRecordId(), user.getAccount());

        }
        model.addAttribute("approver",types);
        model.addAttribute("Proson",namess);



        IPage<Scraprecord> pageInfo = scraprecordService.page(page, queryWrapper);
        model.addAttribute("scrapApplytimeSpace", scrapApplytimeSpace);
        model.addAttribute("scrapCreatetimeSpace", scrapCreatetimeSpace);
        model.addAttribute("searchInfo", scraprecord);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param scraprecord
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Scraprecord scraprecord){
        return toAjax(scraprecordService.save(scraprecord));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("scraprecord",scraprecordService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param scraprecord
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Scraprecord scraprecord){
        return toAjax(scraprecordService.updateById(scraprecord));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(scraprecordService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(scraprecordService.removeByIds(ids));
    }

}

